﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GameScreen.cs" company="">
//   
// </copyright>
// <summary>
//   The game screen.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

/*
 * Author: Juan Mompean Esteban
 * Created: 26-11-2011
 */
namespace ITU_vs_Zombies.Menu
{
    using System.Diagnostics.Contracts;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Media;

    /// <summary>
    /// The game screen.
    /// </summary>
    public abstract class GameScreen
    {
        #region Constants and Fields

        /// <summary>
        /// The song name.
        /// </summary>
        protected string SongName;

        /// <summary>
        /// The is serializable.
        /// </summary>
        private bool isSerializable = true;

        /// <summary>
        /// The other screen has focus.
        /// </summary>
        private bool otherScreenHasFocus;

        /// <summary>
        /// The screen state.
        /// </summary>
        private ScreenState screenState = ScreenState.Hidden;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets a value indicating whether IsActive.
        /// </summary>
        public bool IsActive
        {
            get
            {
                return !this.otherScreenHasFocus && this.screenState == ScreenState.Active;
            }
        }

        /// <summary>
        /// Gets or sets ScreenManager.
        /// </summary>
        public ScreenManager ScreenManager { get; internal set; }

        /// <summary>
        /// Gets or sets ScreenState.
        /// </summary>
        public ScreenState ScreenState
        {
            get
            {
                return this.screenState;
            }

            set
            {
                this.screenState = value;
            }
        }

        /// <summary>
        /// Gets or sets Song.
        /// </summary>
        public Song Song { get; protected set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// The activate.
        /// </summary>
        /// <param name="instancePreserved">
        /// The instance preserved.
        /// </param>
        public virtual void Activate(bool instancePreserved)
        {
            Contract.Requires(this.ScreenManager != null);
        }

        /// <summary>
        /// The draw.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        public virtual void Draw(GameTime gameTime)
        {
            Contract.Requires(gameTime != null);
            Contract.Requires(this.ScreenManager != null);
            Contract.Requires(this.ScreenManager.SpriteBatch != null);
        }

        /// <summary>
        /// The exit screen.
        /// </summary>
        public void ExitScreen()
        {
            this.ScreenManager.RemoveScreen(this);
        }

        /// <summary>
        /// The handle input.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        /// <param name="input">
        /// The input.
        /// </param>
        public virtual void HandleInput(GameTime gameTime, InputState input)
        {
            Contract.Requires(gameTime != null);
            Contract.Requires(input != null);
        }

        /// <summary>
        /// The unload.
        /// </summary>
        public virtual void Unload()
        {
        }

        /// <summary>
        /// The update.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        /// <param name="otherScreenHasFocus">
        /// The other screen has focus.
        /// </param>
        /// <param name="coveredByOtherScreen">
        /// The covered by other screen.
        /// </param>
        public virtual void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            Contract.Requires(gameTime != null);
            this.otherScreenHasFocus = otherScreenHasFocus;
            if (coveredByOtherScreen)
            {
                this.screenState = ScreenState.Hidden;
            }
            else
            {
                this.screenState = ScreenState.Active;
                if (this.Song != null && MediaPlayer.State != MediaState.Playing)
                {
                    MediaPlayer.Play(this.Song);
                    MediaPlayer.IsRepeating = true;
                }
            }
        }

        #endregion
    }
}